# ------------------------- #
#    Create the networks    #
# ------------------------- #

## Clear working space
rm(list = ls())

## Load data
library(peacesciencer)
library(countrycode)
data(atop_alliance)
data(codelist)
dipl_dyad   <- read.csv("data/dipl_dyad.csv")
bla_dyad    <- haven::read_dta("data/bla_dyad.dta")
dca_dyad    <- read.csv("data/dca_dyad.csv")
contiguity  <- read.csv("data/contdir.csv") ## contiguity
pta         <- readxl::read_excel("data/pta.xlsx")

## Format data
dipl_dyad <- mutate(
  dipl_dyad,
  ccode1a = if_else(ccode1 < ccode2, ccode1, ccode2),
  ccode2a = if_else(ccode2 < ccode1, ccode1, ccode2),
  ccode1 = ccode1a, 
  ccode2 = ccode2a,
  ambassador = if_else(DR_at_1 == 3 & DR_at_2 == 3, 1, 0),
  shared_dipl = if_else(DR_at_1 > 0 & DR_at_2  > 0, 1, 0),
  dipl = if_else(DR_at_1 + DR_at_2  > 0, 1, 0)
) %>% dplyr::select(
  c(
    ccode1, ccode2, year, ambassador, shared_dipl, dipl
  )
) %>% distinct(
  year, ccode1, ccode2, .keep_all = T
) 

## Format data
bla_dyad <- filter(
  bla_dyad, 
  !is.na(firstsignedN) 
) %>% distinct(
  code_source, code_host, year
) %>% as.data.frame(.) %>% mutate(
  ccode1a = if_else(code_host < code_source, code_host, code_source),
  ccode2a = if_else(code_source < code_host, code_host, code_source),
  code_host = ccode1a, 
  code_source = ccode2a
) %>% dplyr::select(
  -c(
    ccode1a, ccode2a
  )
)

## DCA data
dca_dyad <- dplyr::select(
  dca_dyad, 
  year, ccode1, ccode2, 
  dcaGeneralV1, dcaGeneralV2, 
  dcaSectorV1, dcaSectorV2,
  dcaAnyV1, dcaAnyV2
) %>% dplyr::rename(
  dca_generalV2 = dcaGeneralV2, 
  dca_sectorV2  = dcaSectorV2,
  
  dca_generalV1 = dcaGeneralV1, 
  dca_sectorV1  = dcaSectorV1,
  
  dcaV1 = dcaAnyV1,
  dcaV2 = dcaAnyV2
  
) %>% mutate(
  ccode1a = if_else(ccode1 < ccode2, ccode1, ccode2),
  ccode2a = if_else(ccode2 < ccode1, ccode1, ccode2),
  ccode1 = ccode1a, 
  ccode2 = ccode2a
) %>% dplyr::select(
  c(
    ccode1, ccode2, year, 
    dca_generalV2, dca_sectorV2,
    dca_generalV1, dca_sectorV1, 
    dcaV1, dcaV2
  )
) %>% distinct(
  year, ccode1, ccode2, .keep_all = T
) 


## Use peace science to create data
download_extdata()
dyad_data <- create_dyadyears(directed = F) %>% add_nmc() %>% add_peace_years() %>% add_cow_mids()  %>% add_cow_majors() %>% add_cow_trade() %>% add_minimum_distance() %>% add_igos()

## Alliance data
atop_alliance <- mutate(
  atop_alliance,
  ccode1a = if_else(ccode1 < ccode2, ccode1, ccode2),
  ccode2a = if_else(ccode2 < ccode1, ccode1, ccode2),
  ccode1 = ccode1a, 
  ccode2 = ccode2a
  
) %>% dplyr::select(
  -c(
    ccode1a,  ccode2a
  )
)

## Create dyad data
dyad_data <- arrange(
  dyad_data, 
  year, ccode1, ccode2
) %>% filter(
  year %in% min(atop_alliance$year):max(atop_alliance$year)
)%>% distinct(
  ccode1, ccode2, year, .keep_all = T
) %>% left_join(
  dca_dyad,
  by = c(
    "ccode1", "ccode2", "year"
  )
) %>% left_join(
  atop_alliance,
  by = c(
    "ccode1", "ccode2", "year"
  )
)

## Impute missing region data
codelist$region23[codelist$cow.name == "Austria-Hungary"] <-"Western Europe"
codelist$region23[codelist$cow.name == "Baden"] <-"Western Europe"
codelist$region23[codelist$cow.name == "Bavaria"] <-"Western Europe"
codelist$region23[codelist$cow.name == "Czechoslovakia"] <-"Eastern Europe"
codelist$region23[codelist$cow.name == "German Democratic Republic"] <-"Eastern Europe"
codelist$region23[codelist$cow.name == "Hanover"] <-"Western Europe"
codelist$region23[codelist$cow.name == "Hesse Electoral"] <-"Western Europe"
codelist$region23[codelist$cow.name == "Hesse Grand Ducal"] <-"Western Europe"
codelist$region23[codelist$cow.name == "Mecklenburg Schwerin"] <-"Western Europe"
codelist$region23[codelist$cow.name == "Modena"] <-"Southern Europe"
codelist$region23[codelist$cow.name == "Parma"] <-"Southern Europe"
codelist$region23[codelist$cow.name == "Saxony"] <-"Western Europe"
codelist$region23[codelist$cow.name == "Tuscany"] <-"Southern Europe"
codelist$region23[codelist$cow.name == "Two Sicilies"] <-"Southern Europe"
codelist$region23[codelist$cow.name == "Wuerttemburg"] <-"Western Europe"
codelist$region23[codelist$cow.name == "Yemen Arab Republic"] <-"Western Asia"
codelist$region23[codelist$cow.name == "Yemen People's Republic"] <-"Western Asia"
codelist$region23[codelist$cow.name == "Yugoslavia"] <-"Southern Europe"
codelist$region23[codelist$cow.name == "Kosovo"] <-"Southern Europe"
codelist$region23[codelist$cow.name == "Zanzibar"] <-"Eastern Africa"

## Recode West Germany to Germany 
codelist_west_germany <- filter(
  codelist, 
  cown == 255
  ) %>% mutate(
    cown = 260,
    cowc = "GFR",
    cow.name = "German Federal Republic"
  )

## Recode unified Korea to Korea 
codelist_korea <- filter(
  codelist, 
  cown == 732
) %>% mutate(
  cown = 730,
  cowc = "KOR",
  cow.name = "Korea"
)

## Bring the data back together
codelist <- bind_rows(
  codelist, 
  codelist_west_germany,
  codelist_korea
)

## Create BLA edge
dyad_data <- mutate(
  dyad_data, 
    bla = if_else(
      paste(ccode1, ccode2, year, sep = "-") %in% paste(bla_dyad$code_host, bla_dyad$code_source, bla_dyad$year, sep = "-"),
      1, 0
    )
)

## Create diplomatic edge
dyad_data <- mutate(
  dyad_data, 
  dipl = if_else(
    paste(ccode1, ccode2, year, sep = "-") %in% paste(subset(dipl_dyad, ambassador == 1)$ccode1, subset(dipl_dyad, ambassador == 1)$ccode2, subset(dipl_dyad, ambassador == 1)$year, sep = "-"),
    1, 0
  )
)

## Create IGO edges
dyad_data <- dplyr::rename(
  dyad_data, 
  igo = dyadigos
)

## Keep only distinct cown
codelist_region <- dplyr::select(
  codelist,
  c(
    cown, cowc, region , cow.name
  )
) %>% filter(
  !is.na(cown)
) %>% distinct(
  cown, .keep_all = T
)

## Merge in region data
dyad_data <- left_join(
  dyad_data,
  codelist_region,
  by = c(
    "ccode1" = "cown"
  )
) %>% left_join(
  codelist_region,
  by = c(
    "ccode2" = "cown"
  )
) %>% dplyr::rename(
  region1 = region.x,
  region2 = region.y,
  cowc1 = cowc.x,
  cowc2 = cowc.y,
  cowname1 = cow.name.x,
  cowname2 = cow.name.y
)

## Merge in trade data
dyad_data <- transform(
  dyad_data,
  trade = smoothflow1 + smoothflow2
)


## Merge in contiguity data
contiguity  <- mutate(
  contiguity, 
  syear = as.numeric(substr(begin, 1, 4)),
  eyear = as.numeric(substr(end, 1, 4))
) %>% mutate(
  Var1 = if_else(statelno < statehno, statelab, statehab),
  Var2 = if_else(statehno < statelno, statelab, statehab),
  namea = Var1,
  nameb = Var2,
  ccode1a = if_else(statelno < statehno, statelno, statehno),
  ccode2a = if_else(statehno < statelno, statelno, statehno),
  statelno = ccode1a, 
  statehno = ccode2a,
  contiguity = as.character(paste0(namea, nameb))
) %>% 
  distinct(
   syear, eyear, conttype, statelno, statehno
  ) 

## Create contiguity edge list
contig_list <- list()
for (i in 1816:2014)
{
  temp_contig <- filter(
    contiguity,
    i >= syear,
    i <= eyear
  ) %>% transform(
    year = i
  ) %>% dplyr::rename(
    ccode1 = statelno,
    ccode2 = statehno
  ) %>% distinct(
    ccode1, ccode2, conttype
  ) %>% transform(
    year = i
  )
  contig_list[[length(contig_list) + 1]] <- temp_contig
}

## Bind in contiguity edge list
contiguity <- do.call(bind_rows, contig_list)
missing_ccode <- unique(c(dyad_data$ccode1, dyad_data$ccode2))[!(unique(c(dyad_data$ccode1, dyad_data$ccode2)) %in% unique(c(contiguity$ccode1, contiguity$ccode2)))]

## Check if anything is wrong with the recoding
any(contiguity$ccode1 > contiguity$ccode2)
any(dyad_data$ccode1 > dyad_data$ccode2)

## Delete any duplicates
contiguity <- distinct(
  contiguity, 
  ccode1, ccode2, year, conttype
)

## Bring in contiguity data
dyad_data <- left_join(
  dyad_data,
  contiguity,
  by = c(
    "ccode1", "ccode2", "year"
  )
)

## Format PTA data
pta <- filter(
  pta,
  grepl("fta", type, ignore.case = T)
)

## Any agreement in the row
pta$pta   <- ifelse(rowSums(pta[,9:112]) > 0, 1, 0)

## Country data
pta <- dplyr::select(
  pta, 
  c(
    cty1, cty2, pta, year
  )
) %>% data.frame(.) 

## Fix different names in the DF
wrong_name <- unique(tolower(c(pta$cty1, pta$cty2)))[!(unique(tolower(c(pta$cty1, pta$cty2))) %in% unique(tolower(c(dyad_data$cowname1,dyad_data$cowname2))))]
pta <- transform(
  pta,
  cty1 = tolower(cty1),
  cty2 = tolower(cty2)
)

## Fix name for merging purposes
pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "antigua and barbuda", "antigua & barbuda", cty1),
  cty2 = ifelse(cty2 == "antigua and barbuda", "antigua & barbuda", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "saint lucia", "st. lucia", cty1),
  cty2 = ifelse(cty2 == "saint lucia", "st. lucia", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "micronesia", "federated states of micronesia", cty1),
  cty2 = ifelse(cty2 == "micronesia", "federated states of micronesia", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "slovak republic", "slovakia", cty1),
  cty2 = ifelse(cty2 == "slovak republic", "slovakia", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "united states", "united states of america", cty1),
  cty2 = ifelse(cty2 == "united states", "united states of america", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "saint vincent and the grenadines", "st. vincent and the grenadines", cty1),
  cty2 = ifelse(cty2 == "saint vincent and the grenadines", "st. vincent and the grenadines", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "kyrgyz republic", "kyrgyzstan", cty1),
  cty2 = ifelse(cty2 == "kyrgyz republic", "kyrgyzstan", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "saint kitts and nevis", "st. kitts and nevis", cty1),
  cty2 = ifelse(cty2 == "saint kitts and nevis", "st. kitts and nevis", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "republic of djibouti", "djibouti", cty1),
  cty2 = ifelse(cty2 == "republic of djibouti", "djibouti", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "cote d'ivoire", "ivory coast", cty1),
  cty2 = ifelse(cty2 == "cote d'ivoire", "ivory coast", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "democratic republic of congo", "democratic republic of the congo", cty1),
  cty2 = ifelse(cty2 == "democratic republic of congo", "democratic republic of the congo", cty2)
)

pta <- transform(
  pta,
  cty1 = ifelse(cty1 == "cabo verde", "cape verde", cty1),
  cty2 = ifelse(cty2 == "cabo verde", "cape verde", cty2)
)

## Create PTA ids
pta_ids <- c(paste0(pta$cty1, pta$cty2, pta$year), paste0(pta$cty2, pta$cty1, pta$year))

## Make mid, fatal mid, contiguity, and pta variables
dyad_data <- distinct(
  dyad_data, 
  ccode1, ccode2, year, .keep_all = T 
) %>% mutate(
  mid = cowmidongoing, 
  fatal_mid = if_else(mid == 1 & fatality > 0, 1, 0),
  contig = if_else(conttype %in% 1:2, 1, 0),
  pta = if_else(tolower(paste0(cowname1, cowname2, year)) %in% pta_ids, 1, 0)
) 

## Save the data
dir.create("intl_order", showWarnings = F, recursive = T)
saveRDS(dyad_data, file = "intl_order/network_layers_replication.rds")

